/**
* © 2010 Autodata Solutions Company. All Rights Reserved
* This source code is the confidential and proprietary information of Autodata Solutions Company.
* The user shall not, in whole or in part, copy, publish, disclose or make any use of this source code
* without the prior written consent of Autodata Solutions Company.
*
* File: com/extjs/extensions/form/text-extended.js
* Purpose: Provides user extensions for the TextField object.
*
* Namespace: net.autodata
* Requires:ExtJS,
*          ASC library - net/autodata/ascjs-ext-debug.js
*/

Ext.ns('net.autodata.form.PostalCodeField');
(function() {
    var DQ = Ext.DomQuery;

    /**
    * Postalcode validation type (vtype) definition
    */
    Ext.apply(Ext.form.VTypes, {
        postalcode: function(val, field) {
            var ret = /^[A-Z]\d[A-Z]\d[A-Z]\d$/.test(val);
            if (ret) {
                ret = val.length == 6;
            }

            return ret;
        },
        postalcodeText: 'Invalid postal code format',
        postalcodeMask: /([A-Z]\d)*/
    });

    /**
    * Custom postal code text field definition
    */
    var Field = Ext.extend(Ext.form.TextField, {
        vtype: 'postalcode',
        width: '80px',
        maxLength: 6,
        allowBlank: false,
        enableKeyEvents: true,
        required: true,

        initComponent: function() {
            Field.superclass.initComponent.call(this, arguments);
            this.vtypeText = ASC.message('ca.autotrader.compare.postalcode.window.error.invalidformat');
            this.on('keyup', this.onKeyUp);
        },

        /**
        * Event handler that enforces the textfield's value to always be uppercase.
        */
        onKeyUp: function() {
            this.setValue(this.getValue().toUpperCase());
        },

        onRender: function() {
            Field.superclass.onRender.apply(this, arguments);

            if (this.el) {
                this.el.set({ maxlength: this.maxLength, maxLength: this.maxLength }, true);
            }
        }
    });

    net.autodata.form.PostalCodeField = Field;

    /**
    * Registers the xtype postalcode to the Ext widget
    */
    Ext.reg('postalcode', net.autodata.form.PostalCodeField);
})();